//
//  GesturePasswordView.swift
//  GesturePassword4Swift
//
//  Created by feiin on 14/11/22.
//  Copyright (c) 2014年 swiftmi. All rights reserved.
//
//  Edited by Glitter on 16/01/12.

import UIKit
 
class GesturePasswordView: UIView, TentacleViewDelegate {
    var forgetClosure:(()->())?
    
    func resetPassword(_ result: String,times:Int) -> Bool {
         return true
    }
    
    func verification(_ result: String) -> Bool {
        return true
    }
    func gestureSetFail() {
        
    }
    

    var tentacleView: TentacleView?

    var state: UILabel?

 
    var imgView: UIImageView?

    var forgetButton: UIButton?
 
   fileprivate var buttonArray: [GesturePasswordButton]=[]
 
    override init(frame: CGRect) {

        super.init(frame: frame)

        // Initialization code

        let view = UIView(frame:CGRect(x: frame.size.width/2-160, y: frame.size.height/2-140, width: frame.size.width, height: frame.size.width))

        for i in 0..<9 {

            let row = Int(i/3)
            let col = Int(i%3)

            let distance = Int(320/3)
            let size: Int = Int(Float(distance)/1.5)
            let margin = Int(size/4)

            let gesturePasswordButton = GesturePasswordButton(frame: CGRect(x: CGFloat(col*distance+margin), y: CGFloat(row*distance), width: CGFloat(size), height: CGFloat(size)))

            gesturePasswordButton.tag = i

            view.addSubview(gesturePasswordButton)
            buttonArray.append(gesturePasswordButton)

        }

        self.addSubview(view)

        tentacleView = TentacleView(frame: view.frame)

        tentacleView!.buttonArray = buttonArray
        tentacleView!.delegate = self
        self.addSubview(tentacleView!)

        imgView = UIImageView(frame:CGRect(x: frame.size.width/2-35, y: frame.size.width/2-110, width: 70, height: 70))
        imgView?.backgroundColor = UIColor.white
        imgView?.image = UIImage(named: "username")
        imgView?.clipsToBounds = true
        imgView!.layer.cornerRadius = 35
        imgView!.layer.borderColor = UIColor.gray.cgColor
        imgView!.layer.borderWidth = 3
        self.addSubview(imgView!)
        self.bringSubviewToFront(imgView!)

        state = UILabel(frame: CGRect(x: 0, y: imgView!.frame.maxY + 10, width: frame.size.width, height: 30))
        state!.textAlignment = .center
        state!.font = UIFont.systemFont(ofSize: 16)
        self.addSubview(state!)

        forgetButton = UIButton(frame:CGRect(x: frame.size.width/2-60, y: frame.size.height/2+220, width: 120, height: 30))
        forgetButton!.titleLabel?.font = UIFont.systemFont(ofSize: 14)
        forgetButton!.setTitleColor(UIColor.white, for: UIControl.State())
        forgetButton!.setTitle("忘记手势密码？", for: UIControl.State())
        forgetButton!.addTarget(self, action: #selector(GesturePasswordView.forget), for: .touchDown)
        self.addSubview(forgetButton!)
 

    }

    required init?(coder aDecoder: NSCoder) {
        super.init(coder: aDecoder)
    }

    // Only override drawRect: if you perform custom drawing.
    // An empty implementation adversely affects performance during animation.
    override func draw(_ rect: CGRect) {
        super.draw(rect)
        // Drawing code

        let context: CGContext = UIGraphicsGetCurrentContext()!

        let rgb: CGColorSpace = CGColorSpaceCreateDeviceRGB()
        let colors: [CGFloat] = [
            134 / 255.0,
            157 / 255.0,
            147 / 255.0,
            1.00,
            3 / 255.0,
            3 / 255.0,
            37 / 255.0,
            1.00
            ]

        let gradient: CGGradient = CGGradient(colorSpace: rgb, colorComponents: colors, locations: nil, count: Int(2))!

        context.drawLinearGradient(gradient, start: CGPoint(x: 0.0, y: 0.0), end: CGPoint(x: 0.0, y: self.frame.size.height), options: .drawsBeforeStartLocation)

    }

    func gestureTouchBegin() {
        self.state!.text = ""
    }

    @objc func forget() {
        if let clo = self.forgetClosure {
            clo()
        }
    }

   

}
